package com.example.edu_ucenter.controller;


import com.example.commonutils.JwtUtils;
import com.example.commonutils.R;
import com.example.commonutils.orderVo.UserOrder;
import com.example.edu_ucenter.entity.UcenterMember;
import com.example.edu_ucenter.entity.vo.UserRegister;
import com.example.edu_ucenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-15
 */

@RestController
@RequestMapping("/edu_ucenter/user")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService service;

    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){

       String token=service.login(member);

       return R.ok().data("token",token);
    }

    @PostMapping("register")
    public R registerUser(@RequestBody UserRegister register){
        service.register(register);

        return R.ok();
    }

    @GetMapping("getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember user = service.getById(userId);
        return R.ok().data("userInfo",user);
    }

    @PostMapping("getUserInfo/{id}")
    public UserOrder getUserInfoById(@PathVariable String id){
        UcenterMember user = service.getById(id);

        UserOrder userOrder = new UserOrder();
        BeanUtils.copyProperties(user,userOrder);

        return userOrder;
    }


    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = service.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }
}

